/**
 * resource.cpp is part of Gedule.
 *
 * Gedule is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gedule is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gedule.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "resource.h"
#include "../utils/visitor/visitor.h"
#include "constraints/resourceconstraint.h"
#include <sstream>
#include <boost/foreach.hpp>

///////////////////////////////////////////////////////////////
Resource::Resource( const std::string & a_resourceName ) :
    Visitable(),
    Printable(),
    m_resourceName( a_resourceName ),
    m_constraints()
{
}

///////////////////////////////////////////////////////////////
Resource::~Resource()
{
    ClearConstraints();
    m_resourceName = UNNAMED;
}

///////////////////////////////////////////////////////////////
void Resource::ClearConstraints()
{
    BOOST_FOREACH( ResourceConstraint * constraint, m_constraints )
    {
        if( constraint )
             delete constraint;
    }

    m_constraints.clear();
}

///////////////////////////////////////////////////////////////
void Resource::Accept( Visitor * a_visitor, bool a_depthFirst )
{
    if( a_visitor )
    {
        if( !a_depthFirst )
            a_visitor->Visit( this );

        VisitConstraints( a_visitor );

        if( a_depthFirst )
            a_visitor->Visit( this );
    }
}

///////////////////////////////////////////////////////////////
void Resource::VisitConstraints( Visitor * a_visitor, bool a_depthFirst )
{
    BOOST_FOREACH( ResourceConstraint * constraint, m_constraints )
    {
        if( constraint )
             constraint->Accept( a_visitor, a_depthFirst );
    }
}

///////////////////////////////////////////////////////////////
std::string Resource::Print() const
{
    std::ostringstream strStream;
    strStream << "Resource : " << m_resourceName << std::endl;
    strStream << PrintConstraints();

    return strStream.str();
}

///////////////////////////////////////////////////////////////
std::string Resource::PrintConstraints() const
{
    std::ostringstream strStream;
    strStream << "Constraints : ";

    BOOST_FOREACH( ResourceConstraint * constraint, m_constraints )
    {
        if( constraint )
             strStream << std::endl << constraint->Print();
    }

    return strStream.str();
}

///////////////////////////////////////////////////////////////
void Resource::ResourceName( const std::string & a_resourceName )
{
    m_resourceName = a_resourceName;
}

///////////////////////////////////////////////////////////////
const std::string & Resource::ResourceName() const
{
    return m_resourceName;
}

///////////////////////////////////////////////////////////////
void Resource::Add( ResourceConstraint * a_constraint )
{
    if( a_constraint )
        m_constraints.insert( a_constraint );
}
